#include <iostream>
#include <algorithm>
using namespace std;

typedef struct hw
{
    int ddl;
    int score;
} Hwk;

bool cmp( Hwk a, Hwk b )
{
    if(a.score != b.score)
    {
        return a.score > b.score;
    }
    else
    {
        return a.ddl < b.ddl;
    }
}

Hwk homeworks[1010];
int main()
{
    ios::sync_with_stdio(false);
    int i, j;
    int T;//number of test cases
    int N; //number of homeworks, up to 1000
    int max_ddl;
    int now;
    long loss;
    bool busy[1010] = {0};
    cin >> T;
    while(T--)
    {
        loss = 0;
        cin >> N;
        for (i = 0; i < N;i++)
        {
            cin >> homeworks[i].ddl;
        }
        for (i = 0; i < N; i++)
        {
            cin >> homeworks[i].score;
            loss += homeworks[i].score;
            busy[i + 1] = false;
        }
        sort(homeworks, homeworks + N, cmp);
        max_ddl = homeworks[0].ddl;
        now = 0;
        for (i=0; i < N;i++)
        {
            for (j = homeworks[i].ddl;j>0;j--)
            {
                if(!busy[j])
                {
                    busy[j] = true;
                    loss -= homeworks[i].score;
                    break;
                }
            }
        }
        cout << loss << endl;
    }
}